﻿//-------------------------------------------------------------
// Copyright © Berico Technologies, LLC. All Rights Reserved
//
// This source is subject to the Microsoft Public License. Please
// visit http://www.microsoft.com/opensource/licenses.mspx#Ms-PL
// for more information.
//
// SnagL™ is a trademark of Berico Technologies.
//-------------------------------------------------------------

using System;
using System.ComponentModel.Composition;
using System.Windows.Input;
using Berico.SnagL.Infrastructure.Clustering;
using Berico.SnagL.Infrastructure.Events;
using Berico.SnagL.Infrastructure.Modularity.Contracts;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace Berico.SnagL.Infrastructure.Modularity.Toolbar
{
    /// <summary>
    /// Contains functionality for the circle toolbar item
    /// </summary>
    [PartMetadata("ID", "ToolbarItemViewModelExtension")]
    [PartMetadata("NAME", "CIRCLE_LAYOUT")]
    [Export(typeof(CircleToolbarItemExtensionViewModel))]
    public class CircleToolbarItemExtensionViewModel : ViewModelBase, IToolbarItemViewModelExtension
    {
        #region Fields

        /// <summary>
        /// Stores the control description
        /// </summary>
        private string description;

        /// <summary>
        /// Stores a value indicating whether or not the control is checked
        /// </summary>
        private bool isChecked;

        /// <summary>
        /// Gets or sets a value indicating whether or not the control is enabled
        /// </summary>
        private bool isEnabled = true;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CircleToolbarItemExtensionViewModel"/> class 
        /// </summary>
        public CircleToolbarItemExtensionViewModel()
        {
            this.Index = 20;
            this.description = "Circle Layout:  Layout the graph in a circular pattern";
            this.isChecked = false;
            this.Name = "CIRCLE_LAYOUT";

            SnaglEventAggregator.DefaultInstance.GetEvent<ClusteringCompletedEvent>().Subscribe(ClusteringCompletedEventHandler, false);
        }

        #endregion

        #region Events

        /// <summary>
        /// Raised when the toolbar item has been selected
        /// </summary>
        public event EventHandler<EventArgs> ToolbarItemSelected;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether or not the control is checked
        /// </summary>
        public bool IsChecked
        {
            get
            {
                return this.isChecked;
            }
            set
            {
                this.isChecked = value;
                RaisePropertyChanged("IsChecked");
            }
        }

        /// <summary>
        /// Gets or sets the index
        /// </summary>
        public int Index { get; set; }

        /// <summary>
        /// Gets or sets the description
        /// </summary>
        public string Description
        {
            get
            {
                return this.description;
            }
            set
            {
                this.description = value;
                RaisePropertyChanged("Description");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether or not the control is enabled
        /// </summary>
        public bool IsEnabled
        {
            get
            {
                return this.isEnabled;
            }

            set
            {
                this.isEnabled = value;
                RaisePropertyChanged("IsEnabled");
            }
        }

        /// <summary>
        /// Gets or sets the name of the control
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// Gets a reference to the <see cref="ICommand"/> executed when the item is selected
        /// </summary>
        public ICommand ItemSelected
        {
            get
            {
                return new RelayCommand(() => this.OnToolbarItemSelected(EventArgs.Empty));
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Handles the ClusteringCompleted event
        /// </summary>
        /// <param name="args">The arguments for the event</param>
        public void ClusteringCompletedEventHandler(ClusteringCompletedEventArgs args)
        {
            IsEnabled = !args.ClusteringActive;
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Handles the event when a toolbar item is selected
        /// </summary>
        /// <param name="e">Any event arguments that might be passed</param>
        protected virtual void OnToolbarItemSelected(EventArgs e)
        {
            if (ToolbarItemSelected != null)
            {
                ToolbarItemSelected(this, e);
            }
        }

        #endregion
    }
}